package com.product.Dao.impl;


import com.product.Dao.ProductDao;
import com.product.pojo.Product;
import com.product.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    /**
     * 插入账户信息
     * @param product 产品信息
     * @return 1表示插入成功
     */
    @Override
    public int insert(Product product) {
        PreparedStatement ps=null;
        int count=0;
        try {
            Connection conn= DBUtil.getConnection();
            String sql="insert into product(id,name,price,type,photo) values(?,?,?,?,?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1, String.valueOf(product.getId()));
            ps.setString(2,product.getName());
            ps.setString(3,product.getPrice());
            ps.setString(4,product.getType());
            ps.setString(5,product.getPhoto());
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,ps,null);
        }
        return count;
    }

    @Override
    public int deleteById(int id) {
        PreparedStatement ps=null;
        int count=0;
        try {
            Connection conn=DBUtil.getConnection ();
            String sql="delete from product where id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, String.valueOf(id));
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,ps,null);
        }
        return count;
    }

    @Override
    public int update(Product product) {
        PreparedStatement ps=null;
        int count=0;
        try {
            Connection conn=DBUtil.getConnection();
            String sql="update product set name=?,price=?,type=? where id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,product.getName());
            ps.setString(2,product.getPrice());
            ps.setString(3,product.getType());
            ps.setString(4, String.valueOf(product.getId()));
            count=ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,ps,null);
        }
        return count;
    }

    @Override
    public Product selectByProductName(String name) {
        PreparedStatement ps=null;
        ResultSet rs=null;
        Product product = new Product();
        try {
            Connection conn=DBUtil.getConnection();
            String sql="select * from product where name=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,name);
            rs=ps.executeQuery();
            if (rs.next()){
                int id=rs.getInt("id");
                String price=rs.getString("price");
                String type=rs.getString("type");
                String photo=rs.getString("photo");
                // 将结果集封装成java对象
                product.setId(id);
                product.setName(name);
                product.setPrice(price);
                product.setType(type);
                product.setPhoto(photo);


            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,ps,rs);
        }
        return product;
    }

    @Override
    public List<Product> selectAll() {
        PreparedStatement ps=null;
        ResultSet rs=null;
        Connection conn=null;
        List<Product> productList=new ArrayList<>();
        try {
            conn=DBUtil.getConnection();
            String sql="select * from product";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                String price=rs.getString("price");
                String type=rs.getString("type");
                String photo=rs.getString("photo");
                //封装对象
                Product product=new Product();
                product.setId(id);
                product.setName(name);
                product.setPrice(price);
                product.setType(type);
                product.setPhoto(photo);
                // 加到List集合
                productList.add(product);



            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(conn,ps,rs);
        }

        return productList;
    }
}
